package com.mlink.vs.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import com.google.gson.Gson;

/**
 * 
 * 百度鹰眼api
 *
 */

public class test {

	static final String ak = "3UOmCfdyx0Cv4eq2heutGn4YTbymf2g3";
	static final String service_id = "150162";
	
	public static void main(String[] args) {
		
		//deleteEntity();
		//addEntity();
		//createdistrictfence();
		//addpoint();
		
		ArrayList<Map> list = new ArrayList<Map>();
		Map<String, String> data = new HashMap<String, String>();
		data.put("EVENTDATE", "2017-10-12 16:52:50");
		data.put("USERID", "likangjie");
		
		list.add(data);
		list.add(data);
		
		Gson gson = new Gson();
		String jsonString = gson.toJson(list);
		System.out.println(jsonString);
		
		
		ArrayList mapList = gson.fromJson(jsonString, ArrayList.class);
		 Iterator lst = mapList.iterator();
		while (lst.hasNext()){
			Map mData = (Map)lst.next();
			System.out.println(mData.get("EVENTDATE"));
			System.out.println(mData.get("USERID"));
		}

	}
	
	//add——添加entity
	static void addEntity(){
		String url = "http://yingyan.baidu.com/api/v3/entity/add";
		HttpClient hc = new HttpClient();
		PostMethod pm = new PostMethod(url);
		pm.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
		pm.setParameter("ak", ak);
		pm.setParameter("service_id", service_id);
		pm.setParameter("entity_name", "尹键");
		pm.setParameter("entity_desc", "人保北京朝阳合作4S店");
		try {
			hc.executeMethod(pm);
			String ret = pm.getResponseBodyAsString();
			System.out.println(ret);
			
		} catch (HttpException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
	}
	
	//delete——删除entity
	static void deleteEntity(){
		String url = "http://yingyan.baidu.com/api/v3/entity/delete";
		HttpClient hc = new HttpClient();
		PostMethod pm = new PostMethod(url);
		pm.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
		pm.setParameter("ak", ak);
		pm.setParameter("service_id", service_id);
		pm.setParameter("entity_name", "yinjian");
		try {
			hc.executeMethod(pm);
			String ret = pm.getResponseBodyAsString();
			System.out.println(ret);
			
		} catch (HttpException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
	}
	
	//createdistrictfence——创建行政区划围栏
	static void createdistrictfence(){
		String url = "http://yingyan.baidu.com/api/v3/fence/createdistrictfence";
		HttpClient hc = new HttpClient();
		PostMethod pm = new PostMethod(url);
		pm.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
		pm.setParameter("ak", ak);
		pm.setParameter("service_id", service_id);
		pm.setParameter("fence_name", "北京市");
		pm.setParameter("monitored_person", "尹键");
		pm.setParameter("keyword", "北京市");
		try {
			hc.executeMethod(pm);
			String ret = pm.getResponseBodyAsString();
			System.out.println(ret);
			
		} catch (HttpException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
	}
	
	//为一个entity上传一个轨迹点 
	static void addpoint(){
		String url = "http://yingyan.baidu.com/api/v3/track/addpoint";
		HttpClient hc = new HttpClient();
		PostMethod pm = new PostMethod(url);
		pm.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
		pm.setParameter("ak", ak);
		pm.setParameter("service_id", service_id);
		pm.setParameter("entity_name", "尹键");
		//pm.setParameter("latitude", "39.94"); 
		pm.setParameter("latitude", "25.00");
		//pm.setParameter("longitude","116.44" ); 
		pm.setParameter("longitude","110.02" );
		pm.setParameter("loc_time",String.valueOf(new Date().getTime()/1000l) );
		pm.setParameter("coord_type_input","bd09ll" );
		
		try {
			hc.executeMethod(pm);
			String ret = pm.getResponseBodyAsString();
			System.out.println(ret);
			
		} catch (HttpException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
	}
	
	
	// 围栏查询 
	 
		static void fencelist(){
			String url = "http://yingyan.baidu.com/api/v3/fence/list?ak=3UOmCfdyx0Cv4eq2heutGn4YTbymf2g3&service_id=150162&monitored_person=尹键";
		}

}
